package com.wanmait.translate.administration.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.translate.administration.service.AdministrationHotelService;
import com.wanmait.translate.dto.ResultData;
import com.wanmait.translate.pojo.AdministrationHotel;
import com.wanmait.translate.utils.CommonMessage;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class LogisticsHotelController {
    @Resource
    private AdministrationHotelService administrationHotelService;

    @GetMapping("/logistics/stayList")
    public ResultData<AdministrationHotel> stayList(Integer page, Integer limit){
        ResultData<AdministrationHotel> rd = null;
        try {
            rd = ResultData.successsWithData(0,administrationHotelService.listAllByPage(1,10));
        } catch (Exception e) {
            e.printStackTrace();
            rd = ResultData.fail(CommonMessage.ERR_MSG);
        }
        return rd;
    }

    @GetMapping("/logistics/stayLists")
    @ResponseBody
    public ResultData<AdministrationHotel> stayLists(Integer page,Integer limit,String name,Integer hotel){
        PageInfo<AdministrationHotel> hotelPageInfo = null;

        if (name != null && !"".equals(name)) {
            System.out.println(name);
            hotelPageInfo = administrationHotelService.listAllByname(page,limit,name);
        }else if (hotel!=null){
            if(hotel==0){
                System.out.println(hotel+"000");
                hotelPageInfo = administrationHotelService.listAllByNotHotel(page,limit);
            }else {
                System.out.println("11111");
                hotelPageInfo = administrationHotelService.listAllByHotel(page,limit);
            }
        }else {
            hotelPageInfo = administrationHotelService.listAllByPage(page, limit);
        }
        ResultData<AdministrationHotel> rd = ResultData.successsWithData(0,hotelPageInfo);
        return rd;
    }

    @GetMapping("/logistics/updateStayList")
    @ModelAttribute("hotel")
    public AdministrationHotel updateStayList(Integer id){
        AdministrationHotel hotel = administrationHotelService.selectOne(id);
        return hotel;
    }

    @PostMapping("/logistics/insertStayList")
    public void insert(AdministrationHotel administrationHotel){
        administrationHotelService.insert(administrationHotel);
    }

    @PostMapping("/logistics/updateList")
    public void update(AdministrationHotel administrationHotel){
        administrationHotelService.update(administrationHotel);
    }

    //时间格式格式化
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));
    }
}
